package com.huiyujinrong.qiangdanbao.utils;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.activity.my.UserInforActivity;
import com.huiyujinrong.qiangdanbao.activity.my.UserNoInforActivity;
import com.huiyujinrong.qiangdanbao.activity.my.UserRechargeActivity;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import quickcore.webview.WebViewFragment;
import quickcore.webview.WebViewListener;


public class WebViewActivity extends AppCompatActivity implements WebContract.View, WebViewListener.TitleListener {
    private static final String URL_TEST = "https://test.doraemoney.com/newCube/index.html?proId=84f04d403a3144baa70e524";
    private static final int REQUEST_CODE = 0x01;
    private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 0x11;
    @BindView(R.id.app_titlebar)
    Toolbar toolbar;
    @BindView(R.id.titlebar_title)
    TextView textView;
    @BindView(R.id.web_guize)
    TextView guize;

    @BindView(R.id.web_qiangdan)
    TextView qiangdan;

    private WebViewFragment wvf;
    @BindView(R.id.web_content)
    WebView webView;
    private ValueCallback<Uri[]> mUploadCallbackAboveL;
    private ValueCallback<Uri> mUploadMessage;
    private String callUrl;

    // 弹窗
    private AlertDialog.Builder builder;
    //
    private AlertDialog alertDialog;

    private Toast t;

    private WebPresenter presenter;

    private int status, mm;

    private boolean isStstus;

    private Bundle bundle;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        presenter = new WebPresenter(this, this);
        presenter.start();

        bundle = getIntent().getExtras();
        mm = bundle.getInt("mm");
        status = bundle.getInt("status");

        setupTitleBar();

        webView.setWebViewClient(new webviewclient());

        webView.loadUrl(bundle.getString("url"));
        Log.i("result", "onCreate: " + bundle.getString("url"));

        webView.getSettings().setJavaScriptEnabled(true);
    }


    private class webviewclient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("/borrowHelp/QDB/rules/free.html")) {
                setResult(RESULT_OK);
                    finish();
            } else {
                view.loadUrl(url);
            }
            return true;
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        if (status == 1) {
            isStstus = true;
            qiangdan.setText("立即抢单");
            qiangdan.setBackgroundColor(Color.parseColor("#1494EF"));
        } else if (status == 2) {
            isStstus = false;
            qiangdan.setBackgroundColor(Color.parseColor("#8593A4"));
            qiangdan.setText("已被抢");
        } else {
            qiangdan.setVisibility(View.GONE);
        }
        switch (mm) {
            case 1:
                qiangdan.setVisibility(View.VISIBLE);
                setTitle(1);
                break;
            case 2:
                qiangdan.setVisibility(View.GONE);
                setTitle(4);
                break;
            case 3:
                qiangdan.setVisibility(View.GONE);
                setTitle(3);
                break;
            case 4:
                MobclickAgent.onEvent(this, "ding_dan_xiangqing");   //UM订单详情
                qiangdan.setVisibility(View.GONE);
                setTitle(2);
                break;
            case 5:
                qiangdan.setVisibility(View.GONE);
                setTitle(2);
                break;
            case 6:
                qiangdan.setVisibility(View.GONE);
                setTitle(6);
                break;
            case 7:
                qiangdan.setVisibility(View.GONE);
                setTitle(7);
                break;
        }
    }

    //抢单
    @OnClick(R.id.web_qiangdan)
    void WEB_QIANGDAN() {
        if (isStstus == true) {
            presenter.getQiangdan(String.valueOf(SharedHelper.get(this, "user_id", "")), String.valueOf(SharedHelper.get(this, "id", "")));
        } else {
//            showToast("已被抢");
        }
    }


    @OnClick(R.id.web_guize)
    void WEB_GUIZE() {
        Intent intent = new Intent(this, WebViewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("url", "http://106.15.102.162/jqb-api/qdb_rules/rules.html");
        bundle.putInt("mm", 2);
        bundle.putInt("status", 3);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 初始化设置标题栏
     */
    private void setupTitleBar() {
        toolbar.setNavigationIcon(R.drawable.btn_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

                if (webView.canGoBack()) {
                    webView.goBack();//返回上一页面
                } else {
                    finish();
                }
            }
        });
    }

    //设置标题
    public void setTitle(int num) {
        switch (num) {
            case 1:
                textView.setText("客户详情");
                guize.setVisibility(View.VISIBLE);
                qiangdan.setVisibility(View.VISIBLE);
                break;
            case 2:
                textView.setText("订单详情");
                guize.setVisibility(View.GONE);
                qiangdan.setVisibility(View.GONE);
                break;
            case 3:
                textView.setText("辉誉抢单宝平台服务协议");
                guize.setVisibility(View.GONE);
                qiangdan.setVisibility(View.GONE);
                break;
            case 4:
                textView.setText("抢单规则");
                guize.setVisibility(View.GONE);
                qiangdan.setVisibility(View.GONE);
                break;
            case 5:
                textView.setText("关于我们");
                guize.setVisibility(View.GONE);
                qiangdan.setVisibility(View.GONE);
                break;
            case 6:
                textView.setText("新朋友见面礼");
                guize.setVisibility(View.GONE);
                qiangdan.setVisibility(View.GONE);
                break;
            case 7:
                textView.setText("辉誉抢单宝");
                guize.setVisibility(View.GONE);
                qiangdan.setVisibility(View.GONE);
                break;
        }
    }


    /**
     * 改写物理按键——返回的逻辑
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            if (webView.canGoBack()) {
//                webView.goBack();//返回上一页面
//                return true;
//            } else {
//                finish();//结束当前页面
//            }
//            finish();//结束当前页面
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void showToast(String str) {
        if (t == null) {
            t = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        } else {
            t.setText(str);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        t.show();
    }


    /**
     * 判断  服务器 返回的  code
     *
     * @param sercode
     */
    @Override
    public void stActivity(int sercode) {
        switch (sercode) {
            case 400://资料不完善
                Intent intent = new Intent(WebViewActivity.this, UserNoInforActivity.class);
                intent.putExtra("status", "未认证");
                startActivity(intent);
                break;
            case 401://未认证
                Intent intent2 = new Intent(WebViewActivity.this, UserNoInforActivity.class);
                intent2.putExtra("status", "未认证");
                startActivity(intent2);
                break;
            case 402://审核中

                break;
            case 403://审核通过
                Intent intent5 = new Intent(WebViewActivity.this, UserInforActivity.class);
                intent5.putExtra("status_num", Integer.parseInt(SharedHelper.get(this, "user_status", 0).toString()));
                startActivity(intent5);
                break;
            case 404://审核不通过
                Intent intent3 = new Intent(WebViewActivity.this, UserNoInforActivity.class);
                intent3.putExtra("status", "审核失败");
                startActivity(intent3);
                break;
            case 500://余额不足
                startActivity(new Intent(WebViewActivity.this, UserRechargeActivity.class));
                break;
        }
    }

    @Override
    public void exit() {
        setResult(101);
        MobclickAgent.onEvent(this, "qiang_dan");  //UM抢单
        finish();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.end();
    }

    @Override
    public void onLoadTitle(String s) {

    }
}
